<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/2 0002
 * Time: 11:03
 */

namespace app\admin\controller;


use app\home\model\BfNewslist;
use app\home\model\BfNewslanmu;
use app\home\model\BfAttachments;


class Newslist extends BaseController{
    
    public function initialize(){
        $this -> _initialize();
    }
    
    public function index(){
        
        if( $this -> request -> get('lanmuid')){
            $id = $this -> request -> get('lanmuid');
            $lanmu = BfNewslanmu :: where(['id' => $id]) -> find();
            $this -> assign('lanmu',$lanmu);
        }
        
        return $this->fetch();
    }
    
    public function getList(){
        $search = [
            'pagesize' => '20',
            'lanmuid' => $this -> request -> get('lanmuid')
        ];
        $list = BfNewslist :: loadlist($search);
        foreach($list as $v){
            $v -> lanmu = $v -> loadlanmu -> name;
            $v -> coverpath = $v -> loadcover -> path;
        }
        $this -> result($list,0,'','json');
    }
 
    public function add(){
        $method = $this -> request -> method();
        $get = $this -> request ->get();
        if($method === 'GET'){
            return $this->fetch();
        }else{
            $post = $this -> request -> post();
            $data = [
                'lanmuid' => $post['lanmuid'],
                'title'=>$post['title'],
                'keyword'=>$post['keyword'],
                'description'=>$post['description'],
                'cover'=>$post['cover'],
                'content'=>$post['editorValue'],
                'create_time'=>time(),
            ];
            BfAttachments :: update(['flag' => 1],['id' => $post['cover']]);
            $id = BfNewslist :: create($data);
            
            if($id){
                $this -> result($id,200,'添加成功','json');
            }else{
                $this -> result($id,0,'添加失败','json');
            }
        }
    }
    
    public function edit(){
        $method = $this -> request -> method();
        $get = $this -> request ->get();
        if($method === 'GET'){
            $data = BfNewslist :: get($get['id']);
            $this->assign('data',$data);
            return $this->fetch();
        }else{
            $post = $this -> request -> post();
            $data = [
                'title'=>$post['title'],
                'keyword'=>$post['keyword'],
                'description'=>$post['description'],
                'cover'=>$post['cover'],
                'content'=>$post['editorValue'],
            ];
                        
            BfAttachments :: update(['flag' => 1],['id' => $post['cover']]);
            $id = BfNewslist :: update($data,['id'=>$post['id']]);
            if($id){
                $this -> result($id,200,'修改成功','json');
            }else{
                $this -> result($id,0,'修改失败','json');
            }
        }
    }
    
    public function upload(){
        $this -> _upload();
        $this -> result($result,200,'上传成功','json');
    }
}